package icu.yougan.youganblogbackend.service.impl;

import icu.yougan.youganblogbackend.common.Result;
import icu.yougan.youganblogbackend.mapper.CategoryMapper;
import icu.yougan.youganblogbackend.model.Category;
import icu.yougan.youganblogbackend.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Result<List<Category>> list() {
        List<Category> categories = categoryMapper.selectList();
        return Result.success(categories);
    }

    @Override
    public Result<Category> getById(Integer id) {
        Category category = categoryMapper.selectById(id);
        if (category == null) {
            return Result.fail("分类不存在");
        }
        return Result.success(category);
    }

    @Override
    public Result<Category> add(Category category) {
        categoryMapper.insert(category);
        return Result.success("添加成功", category);
    }

    @Override
    public Result<Category> update(Category category) {
        Category existingCategory = categoryMapper.selectById(category.getId());
        if (existingCategory == null) {
            return Result.fail("分类不存在");
        }
        categoryMapper.update(category);
        return Result.success("更新成功", category);
    }

    @Override
    public Result<Boolean> delete(Integer id) {
        Category category = categoryMapper.selectById(id);
        if (category == null) {
            return Result.fail("分类不存在");
        }
        categoryMapper.deleteById(id);
        return Result.success(true);
    }
} 